<!DOCTYPE html>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>

<style>
pre, #log {
  position: absolute;
  top: 0;
  left: 200px;
}
iframe {
  width: 200px;
  height: 140px;
  overflow-y: hidden;
}
.spacer {
  height: 700px;
}
</style>

<div class="spacer"></div>
<iframe src="http://localhost:8080/intersection-observer/resources/nesting-cross-origin-subframe.html"></iframe>
<div class="spacer"></div>

<script>

 // These helper functions are taken from
 // intersection-observer/resources/intersection-observer-test-utils.js,
 // which isn't available to http tests.
function waitForNotification(f) {
  requestAnimationFrame(function () {
    setTimeout(function () { setTimeout(f); });
  });
}

function checkRect(actual, expected, description) {
  if (!expected.length)
    return;
  assert_equals(actual.left, expected[0], description + '.left');
  assert_equals(actual.right, expected[1], description + '.right');
  assert_equals(actual.top, expected[2], description + '.top');
  assert_equals(actual.bottom, expected[3], description + '.bottom');
}

function checkJsonEntry(actual, expected) {
  checkRect(
      actual.boundingClientRect, expected.boundingClientRect,
      'entry.boundingClientRect');
  checkRect(
      actual.intersectionRect, expected.intersectionRect,
      'entry.intersectionRect');
  if (actual.rootBounds == 'null')
    assert_equals(expected.rootBounds, 'null', 'rootBounds is null');
  else
    checkRect(actual.rootBounds, expected.rootBounds, 'entry.rootBounds');
  assert_equals(actual.target, expected.target);
}

function checkJsonEntries(actual, expected, description) {
  test(function () {
    assert_equals(actual.length, expected.length);
    for (var i = 0; i < actual.length; i++)
      checkJsonEntry(actual[i], expected[i]);
  }, description);
}

async_test(function(t) {
  var iframe = document.querySelector("iframe");

  assert_equals(window.innerWidth, 800, "Window must be 800 pixels wide.");
  assert_equals(window.innerHeight, 600, "Window must be 600 pixels high.");

  function handleMessage(event) {
    if (event.data.hasOwnProperty('ACK')) {
      waitForNotification(function () { iframe.contentWindow.postMessage({ ACK: 1 }, "*"); },
          "Message acknowledged");
    } else if (event.data.hasOwnProperty('scrollTo')) {
      document.scrollingElement.scrollTop = event.data.scrollTo;
      waitForNotification(function() { iframe.contentWindow.postMessage("", "*"); },
          "document.scrollingElement.scrollTop = " + event.data.scrollTo);
    } else if (event.data.hasOwnProperty('actual')) {
      checkJsonEntries(event.data.actual, event.data.expected, event.data.description);
    } else if (event.data.hasOwnProperty('DONE')) {
      document.scrollingElement.scrollTop = 0;
      t.done();
    } else {
      var description = event.data.description;
      waitForNotification(function() { iframe.contentWindow.postMessage("", "*"); }, description);
    }
  }

  window.addEventListener("message", t.step_func(handleMessage));

  iframe.onload = t.step_func(function() {
    waitForNotification(function() { iframe.contentWindow.postMessage("", "*") }, "setup");
  });
}, "Intersection observer test with target in nested cross-origin subframes, potentially rendered in other processes.");
</script>
